package com.kingdee.jdy.demo;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.jdy.controller.CustomerController;
import com.kingdee.jdy.controller.MaterialController;
import com.kingdee.jdy.controller.SalOrderController;
import com.kingdee.jdy.controller.StockController;
import com.kingdee.jdy.controller.UnitController;
import com.kingdee.jdy.domain.AccountInfo;
import com.kingdee.jdy.util.AccessTokenUtil;
import com.kingdee.jdy.util.AccountInfoUtil;
import java.util.HashMap;

import com.kingdee.jdy.util.HttpUtil;

/**
 *运行main方法可新增一张销售订单
 */
public class saleOrderDemo {
	
	public static void main(String[] args) throws Exception {
	    //数据准备
		String accessToken = AccessTokenUtil.GetAccessToken();
        AccountInfo accountInfo = AccountInfoUtil.GetAccountInfo(accessToken);
        //url参数
        HashMap<String, Object> urlParam = new HashMap<>();
        urlParam.put("access_token", accessToken);
        //header 请求头参数
        HashMap<String, Object> headerParam = new HashMap<>();
        headerParam.put("accountId", accountInfo.getAccountId());
        headerParam.put("groupName", accountInfo.getGroupName());

        //同步单位
        long unitId = UnitController.saveUnit(urlParam, headerParam);

        //同步商品
        long materialid_id = MaterialController.saveMaterial(urlParam, headerParam, unitId);

        //同步客户
        long customerid_id = CustomerController.saveCustomer(urlParam, headerParam);

        //同步仓库
        long stockid_id = StockController.saveStock(urlParam, headerParam);

        //同步销售订单
        JSONObject jsonSalOrder = SalOrderController.prepareSalOrderParam(unitId, customerid_id, materialid_id, stockid_id);
        JSONObject resSalOrder = HttpUtil
            .doPost("http://api.kingdee.com/jdy/sal/sal_order_save", urlParam, headerParam, jsonSalOrder.toJSONString());

        if (resSalOrder != null) {
            JSONObject data = resSalOrder.getJSONObject("data");
            if (data != null) {
                String success = data.getString("success");
                if (success.equalsIgnoreCase("true")) {
                    long successPkId = data.getJSONArray("successPkIds").getLongValue(0);
                    System.out.println("同步销售订单成功！successPkIds=" + successPkId);
                } else {
                    System.out.println("同步销售订单失败！");
                    return;
                }
            }
        }
    }



}
